<?php
/**
 * Created by PhpStorm.
 * 订单管理
 * User: caicm
 * Date: 2017/6/21
 * Time: 14:51
 */

namespace WMApi\Controller;

use WMApi\Model\WmSellerModel;
use WMApi\Model\WmStoreModel;
use WMApi\Service\BscService;
use WMApi\Service\SellerOrderService;
use WMApi\Service\SellerStoreService;

class SellerOrderController extends SellerBaseController {

    /**
     * 检查商家账户余额
     * Created By caicm
     * @param int seller_id 商家id
     */
    public function check_seller_account_money(){
        $seller_id = I('post.seller_id', 0, 'int');
        if(empty($seller_id)){
            returnApiError('参数有误');
        }

        $this->check_seller_money($seller_id);

        returnApiSuccess(null, 'ok');

    }

    /**
     * 查询订单配送费用和配送时间
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int goods_number 商品数量
     * @param int goods_weight 商品数量
     * @param string province 省份
     * @param string city 城市
     * @param string district 区
     * @param string address 地址
     * @param int type 查询类型: 0-配送时间/费用 2-是否超出配送范围
     */
    public function get_order_freight_time(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $goods_number = I('post.goods_number', 0, 'int');
        $goods_weight =  I('post.goods_weight', 0, 'int');
        $province = I('post.province');
        $city = I('post.city');
        $district = I('post.district');
        $address = I('post.address');
        $type = I('post.type', 0, 'int');

        if(empty($seller_id) || empty($store_id) || ($type == 0 && empty($goods_number)) || !in_array($type, array(0,2))){
            returnApiError('参数有误');
        }
        if(empty($province) || empty($city) || empty($district) || empty($address)){
            returnApiError('地址参数有误');
        }


        $this->check_seller_money($seller_id);

        $store_service = new SellerStoreService();
        $store_model = new WmStoreModel();
        $bsc_service = new BscService();

        //处理地址
        $address_data = $store_service->dispose_address($province, $city, $district, $address);

        //物流点取货id
        $scc_id = $store_model->find_scc_id($store_id);
        $state = $bsc_service->get_order_freight_time($address_data['longitude'], $address_data['latitude'], $scc_id, $type, $goods_weight, $goods_number);
        if($state['status'] == 1){
            if($type == 0){
                $data['freight'] = $state['freight'];
                $data['list'] = $state['distribution_time']['list'];
                returnApiSuccess($data);

            }else{
                returnApiSuccess(null, 'is ok');
            }

        }else{
            returnApiError($state['error_msg']);
        }

    }

    /**
     * 商家创建订单
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param string expect_time 期望配送时间
     * @param  int goods_number 商品数量
     * @param  int goods_amount 商品价格
     * @param  int goods_weight 商品重量
     * @param string consignee 收货人姓名
     * @param int mobile 收货人电话
     * @param string postscript 订单备注
     * @param string province 省份
     * @param string city 城市
     * @param string district 区
     * @param string address 地址
     * @param string sign_building 地标
     * @param string address_name 门牌号
     */
    public function add_order(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $expect_time = I('post.expect_time');
        $goods_number = I('post.goods_number', 0, 'int');
        $goods_amount = I('post.goods_amount');
        $goods_weight = I('post.goods_weight');
        $consignee = I('post.consignee');
        $mobile = I('post.mobile');
        $postscript = I('post.postscript');
        $province = I('post.province');
        $city = I('post.city');
        $district = I('post.district');
        $address = I('post.address');
        $sign_building = I('post.sign_building');
        $address_name = I('post.address_name');

        if(empty($mobile) || empty($seller_id) || empty($store_id) || empty($expect_time) || empty($goods_number) || empty($consignee)){
            returnApiError('参数有误');
        }
        if(empty($province) || empty($city) || empty($district) || empty($address) || empty($sign_building) || empty($address_name)){
            returnApiError('地址参数有误');
        }
        if(!is_mobile($mobile)){
            returnApiError('手机格式有误');
        }

        $this->check_seller_money($seller_id);

        $order_service = new SellerOrderService();
        $add_order = $order_service->add_order($seller_id, $store_id, $expect_time, $goods_number, $consignee, $mobile, $postscript, $province, $city, $district, $address, $sign_building, $address_name, $goods_amount, $goods_weight);

        if($add_order){
            returnApiSuccess(array('order_id'=>$add_order), '操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /**
     * 获取订单菜单列表
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     */
    public function get_order_menu_list(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        if(empty($seller_id) || empty($store_id)){
            returnApiError('参数有误');
        }

        $order_service = new SellerOrderService();
        $order_menu = $order_service->get_order_menu_list($seller_id, $store_id);

        returnApiSuccess(array('order_menu'=>$order_menu));

    }

    /**
     * 获取订单列表
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int order_state 订单状态 1-待接单 2-已接单 3-骑手已接单 4-配送中 5-确认收货
     * @param int page 分页
     */
    public function get_order_list(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $order_state = I('post.order_state', 0, 'int');
        $page = I('post.page', 1, 'int');
        if(empty($seller_id) || empty($store_id) || empty($order_state)){
            returnApiError('参数有误');
        }
        if(!in_array($order_state, array(1,2,3,4,5))){
            returnApiError('参数有误');
        }

        $order_service = new SellerOrderService();
        $order_list = $order_service->get_order_list($seller_id, $store_id, $order_state, $page);

        if($order_list['order_list']){
            returnApiSuccess($order_list);
        }else{
            returnApiError('无数据');
        }


    }


    /**
     * 订单操作 接单/拒单/取消配送订单
     * Created By caicm
     * @param int seller_id 商家id
     * @param int store_id 店铺id
     * @param int order_id 订单id
     * @param int type 操作类型 1-接单 2-拒单 3-取消订单
     */
    public function order_operation(){
        $seller_id = I('post.seller_id', 0, 'int');
        $store_id = I('post.store_id', 0, 'int');
        $order_id = I('post.order_id', 0, 'int');
        $type = I('post.type', 0, 'int');
        if(empty($seller_id) || empty($store_id) || empty($type) || empty($order_id)){
            returnApiError('参数有误');
        }
        if(!in_array($type, array(1,2,3))){
            returnApiError('参数有误');
        }

        $order_service = new SellerOrderService();
        $state = $order_service->order_operation($seller_id, $store_id, $order_id, $type);

        if($state){
            returnApiSuccess(null, '操作成功！');
        }else{
            returnApiError('操作失败！');
        }

    }

    /**
     * 检查商家账户余额
     * @param int $seller_id
     * @return bool
     */
    protected function check_seller_money($seller_id=0){
        $seller_model = new WmSellerModel();
        $seller_info = $seller_model->find_seller_info($seller_id);
        if($seller_info['account_money'] < C('store_min_money')){
            returnApiError('您的账户余额不足，请去充值！', array('account_money_type'=>1));
        }
        return true;
    }
}